// pages/decoration/decoration.js
import req from "../../../utils/request"
Page({
  data: {
    records: [] // 初始化为空数组
  },

  onLoad() {
    this.loadRecords();
  },

  // 加载报备记录
  loadRecords() {
    wx.showLoading({ title: "加载中..." });
    
    // 模拟API请求 - 实际项目中替换为wx.request
    setTimeout(async() => {
      // 这里使用您提供的JSON数据作为模拟响应

      const res= await req.get("/property/renovation/list")
      
      const records = this.mapApiDataToRecords(res.rows);
      
      this.setData({ records });
      wx.hideLoading();
    }, 1000);
  },

  onShow() {
    // 在页面显示时刷新数据
    this.loadRecords();
  },

  toDetail(e){
    const record = e.currentTarget.dataset.keyword;
    wx.navigateTo({
      url: '/pages/property/renovation-detail/renovation-detail?id=' + record.id
    });
  },

  // 将API数据映射为前端需要的格式
  mapApiDataToRecords(apiData) {
    return apiData.map(item => {
      // 处理状态标签
      const statusMapping = {
        0: { status: "待审批", type: "primary", label: "提交时间" },
        1: { status: "已通过", type: "success", label: "审批时间" },
        2: { status: "已驳回", type: "danger", label: "审批时间" }
      };
      
      // 处理审批状态（默认待审批）
      const approvalStatus = item.approvalStatus ?? 0;
      const statusInfo = statusMapping[approvalStatus] || statusMapping[0];
      
      return {
        id: item.id,
        submitDate: item.submitDate ? this.formatDate(item.submitDate) : null,
        status: statusInfo.status,
        statusType: statusInfo.type,
        timeLabel: statusInfo.label,
        startDate: item.startDate ? this.formatDate(item.startDate) : null,
        houseAddress: item.houseAddress
      };
    });
  },

  // 日期格式化方法
  formatDate(dateString) {
    if (!dateString) return null;
    const date = new Date(dateString);
    return `${date.getFullYear()}-${(date.getMonth()+1).toString().padStart(2,'0')}-${date.getDate().toString().padStart(2,'0')}`;
  },

  // 其他方法保持不变...
  onRecordClick(e) {
    const recordId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/property/renovation-detail/renovation-detail?id=${recordId}`,
    });
  },

  onViewDetails() {
    wx.navigateTo({
      url: "/pages/decoration-details/decoration-details",
    });
  },

  onSubmit() {
    wx.navigateTo({
      url: "/pages/property/renovation-form/renovation-form",
    });
  },

  onPullDownRefresh() {
    this.loadRecords()
    setTimeout(() => {
      wx.stopPullDownRefresh()
    }, 1500);
  },
})